package com.example.mapper.dao;

import com.example.entity.retail.CommentRetail;
import com.example.entity.retail.FollowRetail;
import com.example.entity.retail.NotificationRetail;
import com.example.entity.retail.PostRetail;
import com.example.entity.retail.dto.CommentPostRetailDTO;
import com.example.entity.retail.dto.DescPostRetailDTO;
import com.example.entity.retail.dto.FollowRetailDTO;
import com.example.entity.retail.dto.ItemPostRetailDTO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Set;

public interface RetailDao {

    @Insert("insert into postRetail (retail_uid,retail_title,retail_content,retail_image,uid,retail_time,retail_category) values (#{retailUid},#{retailTitle},#{retailContent},#{retailImage},#{uid},#{retailTime},#{retailCategory})")
    void add(PostRetail postRetail);

    @Select("select * from postRetail")
    List<PostRetail> selectPostAll();

    @Insert("insert into commentRetail (comment_uid,content,comment_image,comment_user,post_item,comment_time,replyToUid) values (#{commentUid},#{content},#{commentImage},#{commentUser},#{postItem},#{commentTime},#{replyToUid})")
    void addComment(CommentRetail commentRetail);

    @Select("select * from postRetail where retail_uid=#{retailUid}")
    DescPostRetailDTO selectPostDescByUid(String retailUid);

    @Select("select * from commentRetail where post_item =#{postItem} and replyToUid is null")
    List<CommentPostRetailDTO> selectPostCommentByUid(String postItem);

    @Select("select * from commentRetail where replyToUid =#{commentUid}")
    Set<CommentRetail> selectCommentReplyByUid(String commentUid);

    @Select("select * from postRetail where retail_uid=#{retailUid}")
    PostRetail selectPostByUid(String retailUid);

    @Select("select * from postRetail where uid=#{uid}")
    List<PostRetail> selectPostByUserUid(String uid);

    @Select("select itemImg,itemName,itemPrice from items where itemId=#{itemId}")
    ItemPostRetailDTO selectItemById(String itemId);

    @Insert("insert into notifications (notiUid,image,uid,content,type,status,created_at,updated_at,toUid) values (#{notiUid},#{image},#{uid},#{content},#{type},#{status},#{createdAt},#{updatedAt},#{toUid})")
    void addNotification(NotificationRetail notificationRetail);

    @Select("select * from notifications where toUid=#{uid}")
    List<NotificationRetail> selectNotificationByUid(String uid);

    @Select("select isLiked from LikeRetail where like_uid=#{uid} and postRetailUid=#{retailUid}")
    Boolean isPostLike(String uid,String retailUid);

    @Select("select count(like_uid) from LikeRetail where postRetailUid =#{retailUid}")
    Integer selectPostLikeCount(String retailUid);

    @Insert("insert into LikeRetail (like_uid,postRetailUid,isLiked) values (#{uid},#{content},1)")
    void addPostLike(String uid,String content);

    @Delete("delete from LikeRetail where like_uid=#{uid} and postRetailUid=#{content}")
    void deletePostLike(String uid,String content);

    @Update("update notifications set status=1 where notiUid=#{notiUid} and toUid=#{uid}")
    void isRead(String uid,String notiUid);

    @Update("update notifications set status=1 where toUid=#{uid}")
    void isAllRead(String uid);

    @Delete("delete from notifications where notiUid=#{notiUid} and toUid=#{uid}")
    void deleteNotification(String uid,String notiUid);

    @Insert("insert into follows (followTo,toFollow) values (#{followTo},#{toFollow})")
    void follow(String followTo,String toFollow);

    @Delete("delete from follows where followTo=#{followTo} and toFollow=#{toFollow}")
    void disFollow(String followTo,String toFollow);

    @Select("select count(followTo) from follows where followTo=#{followTo} and toFollow=#{toFollow}")
    Integer isFollow(String followTo,String toFollow);

    @Select("select count(case when followTo=#{uid} then 1 end) as followToNum,count(case when toFollow=#{uid} then 1 end) as toFollowNum from follows")
    FollowRetailDTO selectFollows(String uid);
}
